package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.core.config.storage.StorageService;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSStorage;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 对象存储服务
 */
@RestController
@RequestMapping("/haopyYueAi/storage")
@Validated
@Api(tags = "上传接口")
public class WxStorageController {

    @Autowired
    private StorageService storageService;


    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        boolean checkFileFormat = storageService.checkFormats(originalFilename);
        if (!checkFileFormat) {
            return ResponseUtil.fail("不支持的文件格式");
        }

        HOSStorage hcStorage = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
        return ResponseUtil.ok(hcStorage);
    }


}
